package com.neusoft.liduan.d0921;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class IO流分类 {
	/*	File
	 * 		表示硬盘上的一个文件的类，有关于文件的基本信息，比如长度、创建、删除、路径信息等行为，不具备读写能力
	 * 	输入与输出流，是以电脑内存为参照的
	 * 		输入流：从硬盘文件读取内容到内存中，读
	 * 			字节流：以byte为单位，可以操作所有类型的文件，比如图片、文本文件、Office文档、电影视频等都可以
	 * 				InputStream，是所有输入流的abstract父类，真正干活的是子类
	 * 				常用子类
	 * 				FileInputStream
	 * 					FileInputStream(File file)
	 * 					FileInputStream(String name)
	 * 					read()：返回一个字节，了解即可
	 * 					read(byte[] bs)：将读取的内容保存在bs数组中
	 * 					read(byte[] bs, off, len)：off从哪里开始，读取len长度
	 * 					close()：关闭流，断开与硬盘文件的连接，释放内存资源，不关闭，不释放，除非程序停止
	 * 			字符流：以char为单位，只能操作文本文件，比较方便。
	 * 				Reader
	 * 				常用子类
	 * 				FileReader
	 * 			缓冲流：复制大文件时，提高效率
	 * 				BufferedInputStream
	 * 				BufferedReader
	 * 			对象流
	 * 				ObjectInputStream
	 * 		输出流：从内存中写入内容到硬盘文件中，写
	 * 			字节流
	 * 				OutputStream，是所有输出流的abstract父类
	 * 				FileOutputStream
	 * 			字符流
	 * 				Writer
	 * 				FileWriter
	 * 			缓冲流
	 * 				BufferedOutputStream
	 * 				BufferedWriter
	 * 			对象流
	 * 				ObjectOutputStream
	 * 					只有实现了java.io.Serializable序列化接口的类才可以使用对象流写入到文件中
	 * 					否则抛出java.io.NotSerializableException异常
	 * 		IO流的效率与每次操作的数组大小有关
	 * 		转换流
	 * 			将字节流转换字符流
	 * 		中文乱码：
	 * 			1、编码不一致造成的
	 * 			2、数据库中的编码、Java连接数据库的编码、JSP页面的编码、Servlet的
	 * 			3、在GBK中文为2个字节，UTF-8是3个字节
	 * 	作业
	 * 		1、在API文档中查看IO流相关类
	 * 		2、分别使用字节流和字符流实现，文本文件、图片、视频，doc无图片，doc有图片的复制，
	 * 		3、在作业2的基础上理解字节流与字符的差异		
	 * 		4、封装一个File类，文件的创建、文件的复制、重命名
	 * 			
	 * 			
	 * 			
	 */
	public static void main(String[] args) {
//		t6();
//		t7();
		t12();
	}

	public static void t12() {
		String src = "files" + File.separator + "4.txt";
		Student s = new Student("张三", 20);
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream(src);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(s);
			fis = new FileInputStream(src);
			ois = new ObjectInputStream(fis);
			Object obj = ois.readObject();
			System.out.println(obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void t11() {
		String inputPath = "files" + File.separator + "1.txt";
		String outputPath = "files" + File.separator + "3.txt";
		t10(inputPath, outputPath);
	}
	
	public static void t10(String src, String dect) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		InputStreamReader isr = null;
		OutputStreamWriter osw = null;
		try {
			File fSrc = new File(src);
			fis = new FileInputStream(src);
			isr = new InputStreamReader(fis, "GBK");
			fos = new FileOutputStream(dect, true);
			osw = new OutputStreamWriter(fos, "GBK");
			int len = -1;
			char[] b = new char[1024];
			while((len = isr.read(b)) != -1) {
				System.out.println(new String(b, 0, len));
				osw.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void t9() {
		long t1 = System.currentTimeMillis();
		String src = "E:\\我的上课视频\\FY20_广州_高校个体_java5班\\liduan\\025-作业讲解二-第五天.mp4";
		String dect = "C:\\Users\\86185\\Desktop\\图片" + File.separator;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			File fSrc = new File(src);
			fis = new FileInputStream(src);
			bis = new BufferedInputStream(fis);
			File f = new File(dect);
			System.out.println(f.isDirectory());
			if(f.isDirectory()) {
				dect += File.separator + fSrc.getName();
			}
			fos = new FileOutputStream(dect, true);
			bos = new BufferedOutputStream(fos);
			int len = -1;
			byte[] b = new byte[2];
			while((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("File文件流复制时间：" + (t2 - t1));
	}
	
	public static void t8() {
		long t1 = System.currentTimeMillis();
		String src = "E:\\我的上课视频\\FY20_广州_高校个体_java5班\\liduan\\025-作业讲解二-第五天.mp4";
		String dect = "C:\\Users\\86185\\Desktop\\图片" + File.separator;
		copy(src, dect);
		long t2 = System.currentTimeMillis();
		System.out.println("File文件流复制时间：" + (t2 - t1));
	}
	
	public static void t7() {
		String src = "C:\\Users\\86185\\Desktop\\图片\\3Dmark.png";
		String dect = "files" + File.separator;
		copy(src, dect);
	}
	
	public static void t6() {
		String inputPath = "files" + File.separator + "1.txt";
		String outputPath = "files" + File.separator + "/3/2.txt";
		copy(inputPath, outputPath);
	}
	
	public static void copy(String src, String dect) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			File fSrc = new File(src);
			fis = new FileInputStream(src);
			File f = new File(dect);
			System.out.println(f.isDirectory());
			if(f.isDirectory()) {
				dect += File.separator + fSrc.getName();
			}
			fos = new FileOutputStream(dect, true);
			int len = -1;
			byte[] b = new byte[1024];
			while((len = fis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void t5() {
		String path = "files" + File.separator + "1.txt";
		FileWriter fw = null;
		try {
			//FileOutputStream(String name, boolean append)append是否附加
			fw = new FileWriter(path, true);
			fw.write("FileWriter这是新内容");//支持字符串
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void t4() {
		String path = "files" + File.separator + "1.txt";
		FileOutputStream fos = null;
		try {
			//FileOutputStream(String name, boolean append)append是否附加
			fos = new FileOutputStream(path, true);
			fos.write("这是新内容".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void t1() {
		String path = "files" + File.separator + "1.txt";//该代码不会触发异常，可以不放try语句中
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);//该代码会触发异常，所以必须放在try语句中
//			int input = fis.read();
//			System.out.println(input);//ASCII码值
//			System.out.println((char)input);
			byte[] b = new byte[3];//实际开发中，一般一次读取1KB
			int len = -1;//保存当前读取的长度
			while((len = fis.read(b)) != -1) {
				System.out.println(new String(b, 0, len));
//				System.out.println(new String(b));
			}
//			int len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
//			len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
//			len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
//			len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
//			len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
//			len = fis.read(b);
//			System.out.println(new String(b));
//			System.out.println(len);
			//在上面的基础上，实现读取整个文件，并打印？
			
//			fis.read(b, off, len)
		}  catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
//		catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
		
	}
	
	public static void t2() {
		//正规的IO写法
		//1、准备文件路径
		String path = "files" + File.separator + "1.txt";//该代码不会触发异常，可以不放try语句中
		//2、定义流对象
		FileInputStream fis = null;
		try {
			//3、初始化流对象
			fis = new FileInputStream(path);//该代码会触发异常，所以必须放在try语句中
			//4、定义读取的数组
			byte[] b = new byte[3];//实际开发中，一般一次读取1KB
			int len = -1;//保存当前读取的长度
			//5、循环读取文件内容
			while((len = fis.read(b)) != -1) {
				System.out.println(new String(b, 0, len));
			}
		}  catch (IOException e) {
			e.printStackTrace();
		} finally {
			//6、关闭文件连接，释放资源
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void t3() {
		String path = "files" + File.separator + "1.txt";
		FileReader fr = null;
		try {
			fr = new FileReader(path);
			char[] c = new char[3];
			int len = -1;
			while((len = fr.read(c)) != -1) {
				System.out.println(new String(c, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
